package com.compass.doctor.ui.chat.attachment

import com.compass.doctor.R
import com.compass.framework.constant.INQUIRY_CODE
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.getStringFromResource
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.netease.yunxin.kit.corekit.im.custom.CustomAttachment
import org.json.JSONObject

/**
 * Description： 自定义问诊消息
 * Date:2023/11/29 14:58
 */
class InquiryAttachment : CustomAttachment(INQUIRY_CODE) {
    private var pictures: ArrayList<String> = ArrayList()

    private var diseaseDescription: String? = ""//病情描述(图片)
    private var typeName: String? = ""//（问诊类型名称）
    private var ifReturnVisit: Int? = 0//是否复诊
    private var illnessDuration: String? = ""//患病时长
    private var lastDiagnosis: String? = ""//上次确诊诊断
    private var lastVisitHospital: String? = ""//上次就医医院
    private var lastVisitDate: Long? = null//上次就诊日期
    private var ifSanity: Int? = 0//上次是否神志清楚
    private var inHospitalHere: Int? = 0//曾在本院住院
    private var inpatientDepartment: String? = ""//住院科室
    private var inpatientDate: Long? = null//住院日期
    private var patientAge: String? = "" //患者年龄
    var describe: String? = ""//病情描述
    var patientName: String? = "" //患者姓名
    var sex: String? = "" //患者性别
    var patientId: String? = "" //患者id
    var patientAvatar: String? =null//头像


    //重写数据解析方法，这里面数据以JSONObject格式进行传输，所以您可以制定自己的数据格式。
    //这个示例表示，自定义数据格式为{"customer_message_key":"我是一个自定义消息"}
    override fun packData(): JSONObject {
        var data = JSONObject()
        data.put("patientName", patientName)
        data.put("typeName", typeName)
        data.put("patientAge", patientAge)
        data.put("sex", sex)
        data.put("diseaseDescriptionText", describe)
        data.put("patientId", patientId)
        data.put("patientAvatar", patientAvatar)
        data.put("diseaseDescription", diseaseDescription)
        data.put("ifReturnVisit", ifReturnVisit)
        data.put("illnessDuration", illnessDuration)
        data.put("lastDiagnosis", lastDiagnosis)
        data.put("lastVisitHospital", lastVisitHospital)
        data.put("lastVisitDate", lastVisitDate)
        data.put("ifSanity", ifSanity)
        data.put("inHospitalHere", inHospitalHere)
        data.put("inpatientDepartment", inpatientDepartment)
        data.put("inpatientDate", inpatientDate)
        return data
    }


    override fun parseData(jsonObject: JSONObject?) {
        // 从接收到的消息内容中解析您的数据
        jsonObject?.let {
            this.patientName = jsonObject.getString("patientName")
            this.typeName = jsonObject.getString("typeName")
            this.patientAge = jsonObject.getString("patientAge")
            this.sex = jsonObject.getString("sex")
            this.describe = jsonObject.getString("diseaseDescriptionText")
            this.patientId = jsonObject.getString("patientId")
            this.patientAvatar = jsonObject.getString("patientAvatar")
            this.diseaseDescription = jsonObject.getString("diseaseDescription")
            this.ifReturnVisit = jsonObject.getInt("ifReturnVisit")
            this.illnessDuration = jsonObject.getString("illnessDuration")
            this.lastDiagnosis = jsonObject.getString("lastDiagnosis")
            this.lastVisitHospital = jsonObject.getString("lastVisitHospital")
            this.lastVisitDate = jsonObject.getLong("lastVisitDate")
            this.ifSanity = jsonObject.getInt("ifSanity")
            this.inHospitalHere = jsonObject.getInt("inHospitalHere")
            this.inpatientDepartment = jsonObject.getString("inpatientDepartment")
            this.inpatientDate = jsonObject.getLong("inpatientDate")

        }

    }


    /**
     * 设置问诊单数据
     */
    fun setInquiryData(
        patientName: String,
        age: String,
        describe: String,
        sex: String,
        patientId: String
    ) {
        this.patientName = patientName;
        this.patientAge = age;
        this.describe = describe;
        this.sex = sex;
        this.patientId = patientId;

    }


    override fun getContent(): String? {
        return getStringFromResource(R.string.attachment_inquiry_msg)
    }


    fun getDiseaseDescription(): List<String> {
        if (diseaseDescription.isNullOrEmpty()) {
            return pictures
        }
        val gson = Gson()
        val type = object : TypeToken<ArrayList<String?>?>() {}.type
        return gson.fromJson(diseaseDescription, type)
    }

    fun getIllnessDuration(): String? {
        if (!illnessDuration.isNullOrEmpty()) {
            return illnessDuration
        }
        return "暂无"

    }

    fun getIfReturnVisit(): String {
        return when (ifReturnVisit) {
            1 -> {
                "是"
            }

            else -> {
                "否"
            }
        }
    }

    fun getLastDiagnosis(): String? {
        if (!lastDiagnosis.isNullOrEmpty()) {
            return lastDiagnosis
        }
        return "暂无"

    }


    fun getPatientAge(): String? {

        if (!patientAge.isNullOrEmpty()) {
            return patientAge
        }
        return "0岁"
    }

    fun getLastVisitHospital(): String? {
        if (!lastVisitHospital.isNullOrEmpty()) {
            return lastVisitHospital
        }
        return "暂无"

    }

    fun getLastVisitDate(): String? {


        lastVisitDate?.let {
            return DateUtils.conversionTime(it, DateUtils.FORMAT_TWO)
        }
        return "暂无"
    }

    fun getIfSanity(): String {

        return when (ifSanity) {
            1 -> {
                "是"
            }

            else -> {
                "否"
            }
        }
    }

    fun getInHospitalHere(): String {

        return when (inHospitalHere) {
            1 -> {
                "是"
            }

            else -> {
                "否"
            }
        }
    }

    fun getInpatientDepartment(): String? {
        if (!inpatientDepartment.isNullOrEmpty()) {
            return inpatientDepartment
        }
        return "暂无"

    }

    fun getITypeName(): String? {
        if (!typeName.isNullOrEmpty()) {
            return typeName
        }
        return "问诊单"

    }

    fun getInpatientDate(): String {
        inpatientDate?.let {
            return DateUtils.conversionTime(it, DateUtils.FORMAT_TWO)
        }
        return "暂无"

    }


}